<template>
  <div class="searchPage">
    <div class="container">
      <div class="top">
        <div class="mainpage" @click="goShoppingMall">
          生活商城
        </div>
        <div class="test">
          <div class="personnal" v-if="DSuser" @click="goMember">个人中心</div>
          <div class="personnal" v-if="!DSuser" @click="goLogin">登录&nbsp;&nbsp;|</div>
          <div class="personnal" v-if="!DSuser" @click="goReginster">注册</div>
          <div class="shop">
            <img :src="shop" width="40px">
            <div class="cart" @click="goCart">购物车({{this.cartNumber}})</div>
          </div>
        </div>
      </div>
    </div>
    <div class="shoppingMall">
      <div class="seach">
        <img :src="locationIcon" width="100px">
        <!-- 商品导航 -->
        <div class="fruite-nav" @click="goCategoryList">
          <div v-for="img in fruiteNav" :key="img.image">
            <img v-lazy="img.image" width="30px" />
            <span class="fruite-des">{{ img.mallCategoryName }}</span>
          </div>
        </div>
        <div class="input" @keyup.enter="getSearchResult()">
          <el-col :span="18">
            <el-autocomplete
              class="inline-input"
              v-model="keyWord"
              :fetch-suggestions="querySearch"
              placeholder="请输入搜索词"
              @select="handleSelect"
            ></el-autocomplete>
          </el-col>
          <div class="seach-img">
            <img :src="searchIcon" width="20px" @click="getSearchResult()">
          </div>
        </div>
      </div>
      <div class="hotword">
        <div class="hot">热门搜索词：</div>
        <div class="word">
          <div @click="getSearchResultFromHot(item.word)" class="www" v-for="(item,index) in hotword" :key="index">{{item.word}}</div>
        </div>
    </div>
    <!-- 搜索结果商品区域 -->
    <div class="searchResult-title">搜索结果：</div>
    <div class="searchResult" v-if="!resultNull">
      <div v-for="(item, index) in searchResult" :key="index">
        <goods-search :goods="item"></goods-search>
      </div>
    </div>
    <div class="restultNull" v-if="resultNull">没有搜索结果</div>
    </div>
    
  </div>
  
</template>

<script>
import { Toast } from 'vant';
import { mapState } from 'vuex';
import axios from 'axios';
import url from '@/serviceAPI.config.js';
import goodsSearch from '../common/goodsSearch';
import goodsComponent from "../common/goods";
export default {
  name: "SearchPage",
  data() {
    return {
      locationIcon: require("../../assets/image/life.png"),
      shop: require('../../assets/image/shop.png'),
      searchIcon: require("@/assets/image/search.png"),
      fruiteNav: [], // 水果导航
      keyWord: '',// 搜索词
      localsearchword: [],// 本地搜索历史
      hotword: [],// 热门搜索词
      searchResult: [], // 搜索结果
      resultNull: false
    }
  },
  created() {
    // 获取到路由传递过来的搜索词
    this.keyWord = this.$route.query.keyWord;
    // 获取首页的分类导航数据
    axios({
      url: url.getShoppingMallInfo,
      method: "get"
    })
    .then(res => {
      if (res.status === 200) {
        console.log("获取导航数据成功");
        let data = res.data.data;
        
        this.fruiteNav = data.category;
        
      }
    })
    .catch(error => {
      console.log(error);
    });
    this.getSearchResult();// 获取搜索结果
    this.gethotword(); // 获取热门搜索词
    // 读取本地搜索记录
    this.localsearchword = localStorage.historyWord
        ? JSON.parse(localStorage.historyWord)
        : [];
  },
  computed: {
    ...mapState(['cartNumber']),
    ...mapState(['DSuser']),
  },
  components: {
      goodsSearch
  },
  methods: {
    gethotword(){
      axios({
        url:url.gethotword,
        method: 'get',
      }).then(res => {
        if(res.data.code == 200){
          // this.hotword = res.data.hotword
          console.log("热门搜索词");
          console.log(res.data.hotword);
          this.hotword = res.data.hotword;
        }
      })
    },
    handleSelect(item) {
        console.log(item);
      },
    querySearch(queryString, cb) {
      var results = [];
      this.localsearchword.map(item =>{
        results.push({
          "value": item
        })
      });
         var results = queryString ? results.filter(this.createFilter(queryString)) : results;
        // 调用 callback 返回建议列表的数据
        cb(results);
      },
      createFilter(queryString) {
        return (restaurant) => {
          return (restaurant.value.indexOf(queryString) === 0);
        };
      },
    // 获取搜索结果
    getSearchResult() {
      axios({
        url: url.getGoodsInfoByKeyWord,
        method: 'post',
        data: { keyWord: this.keyWord }
      }).then((res) => {
        console.log(res)
        if(res.data.code === 200) {
          this.searchResult = res.data.message;
          console.log(this.searchResult);
          if(this.searchResult.length == 0){
            this.resultNull = true;
            this.$message.warning("无搜索结果");
          }else {
            this.resultNull = false;
          }
          axios({
            url:url.postkeyword,
            method: 'post',
            data: {
              keyword: this.keyWord
            }
          }).then(res => {
            if(res.data.code == 200){
              console.log("搜索关键词提交成功");
              this.gethotword();
            }
          })
        }
      })
    },
    // 点击热门搜索词获取搜索结果
    getSearchResultFromHot(i) {
      console.log(i);
      axios({
        url: url.getGoodsInfoByKeyWord,
        method: 'post',
        data: { keyWord: i }
      }).then((res) => {
        console.log(res)
        if(res.data.code === 200) {
          this.searchResult = res.data.message;
          console.log(this.searchResult);
          if(this.searchResult.length == 0){
            this.resultNull = true;
            this.$message("无搜索结果");
          }else {
            this.resultNull = false;
          }
          axios({
            url:url.postkeyword,
            method: 'post',
            data: {
              keyword: this.keyWord
            }
          }).then(res => {
            if(res.data.code == 200){
              console.log("搜索关键词提交成功");
              this.gethotword();
            }
          })
        }
      }).catch((err) => {
        Toast("服务器错误啦")
        console.log(err);
      });
    },
    goCart(){
        this.$router.push({name: 'cart'});
      },
    goMember(){
      this.$router.push({name: 'member'});
    },
    goLogin(){
      this.$router.push({name: 'login'})
    },
    goReginster(){
      this.$router.push({name: 'register'})
    },
    goShoppingMall(){
      this.$router.push({name: 'shoppingMall'})
    },
    goCategoryList() {
      this.$router.push({ name: "categoryList" });
    },
    goDetail(goodsId) {
      this.$router.push({ name: "DetailGoodsInfo", query: { goodsId } });
    }
  },
};
</script>

<style scoped>
.container {
  width: 100%;
  height: 40px;
  background-color: #e5017d;
  margin-bottom: 20px;
}
.top {
  width: 1226px;
  height: 100%;
  margin: 0 auto;
  /* background-color: #; */
  position: relative;
}
.mainpage {
  position: absolute;
  font-size: 22px;
  /* color: #fff; */
  top: 9px;
  left: 10px;
}
.test {
  position: absolute;
  right: 0;
}
.shop {
  display: inline-block;
}
.personnal {
  display: inline-block;
  font-size: 16px;
  margin-right: 10px;
  line-height: 40px;
}
.cart {
  display: inline-block;
  font-size: 16px;
  margin-top: 2px;
}
.shoppingMall {
  width: 1226px;
  margin: 0 auto;
  padding-bottom: 50px;
}
.seach {
  width: 1226px;
  margin: 0 auto 20px;
  position: relative;
}
.input {
  height: 49px;
  position: absolute;
  right: 0;
  top: 0;
}
.seach-input {
  line-height: 48px;
  font-size: 16px;
  padding: 0 10px;
  border: 1px solid #ccc;
  padding-right: 52px;
}
.seach-img {
  /* display: inline-block; */
  position: absolute;
  top: -2px;
  right: 42px;
  /* border: 1px solid #ccc; */
  height: 40px;
  width: 52px;
  padding-top: 13px;
  padding-left: 13px;
  box-sizing: border-box;
}
.search-row {
  overflow: hidden;
  background-color: #e5017d;
  height: 2.2rem;
  line-height: 2.2rem;
}
.search-icon {
  padding-top: 0.5rem;
  padding-left: 0.6rem;
}
.search-button {
  display: flex;
  height: 100%;
  justify-content: center;
  align-items: center;
}
.fruite-nav {
  display: inline-block;
  width: 500px;
  margin-left: 30px;
}
.fruite-nav div {
  display: inline-block;
  margin-right: 10px;
}
.fruite-nav .fruite-des {
  display: inline-block;
  margin-top: 0.2rem;
}
.hotword {
  border-bottom: 1px solid #ccc;
  padding-bottom: 20px;
}
.hot {
  display: inline-block;
  font-size: 22px;
  padding-left: 10px;
}
.word {
  display: inline-block;
  padding-left: 30px;
  padding-top: 30px;
  margin-top: 10px;
}
.www {
  /* height: 30px; */
  display: inline-block;
  margin-right: 10px;
  margin-bottom: 10px;
  padding: 10px;
  border-radius: 10px;
  background-color: #e5017d;
  font-size: 22px;
  cursor: pointer;
}
.searchResult-title {
  color: #e5017d;
  font-size: 22px;
  padding: 0.3rem 30px;
  height: 0.9rem;
  line-height: 0.9rem;
  /* text-align: center; */
}
.searchResult {
  /* padding: 0 0.8rem; */
  display: flex;
  flex-wrap: wrap;
  padding-left: 130px;
}
.restultNull {
  font-size: 22px;
  margin-left: 500px;
}
</style>
